In [1]:
import os
import sampler
import numpy as np
%matplotlib inline
In [2]:
obs = sampler.dataframe_observations()
print(obs.head())
In [3]:
sampler.plot_data(obs.lat, obs.lon, obs.temp, overlay=False, S=50)
Out[3]:
In [4]:
tree = sampler.sample_tree(obs.lat.values, obs.lon.values)
In [5]:
lons, lats = np.meshgrid(np.linspace(105.5, 160.4375, 1000),
np.linspace(-49.5, -4.0625, 1000))
In [20]:
def estimate(tree, obs, coords):
distance, obs_values, _ = sampler.nearest_neighbour(
tree,
obs.temp.values,
coords[0],
coords[1],
K=10)
weights = 1. / distance**2
temp_vector = (np.nansum((weights * obs_values), axis=1) /
np.nansum(weights, axis=1))
return temp_vector, obs_values
In [21]:
%%time
temp_vector, _values = estimate(tree, obs, (lats, lons))
In [22]:
ax = sampler.plot_data(lats, lons, np.reshape(temp_vector, lons.shape))
sampler.plot_data(obs.lat, obs.lon, obs.temp, S=50, ax=ax, cbar=False)
Out[22]:
In [12]:
N = 20
lat_chunks = np.array_split(lats, N)
lon_chunks = np.array_split(lons, N)
In [13]:
%timeit estimate(tree, obs, (lat_chunks[0], lon_chunks[1]))
In [14]:
from IPython.parallel import Client
c = Client(profile='default')
direct = c.direct_view()
balanced = c.load_balanced_view()
In [15]:
@balanced.remote()
def parallel_estimate(tree, obs, coords):
import sys; sys.path.append(notebook_path)
import sampler
import numpy as np
distance, obs_values, _ = sampler.nearest_neighbour(
tree,
obs.temp.values,
coords[0],
coords[1],
K=10)
weights = 1. / distance**2
temp_vector = (np.nansum((weights * obs_values), axis=1) /
np.nansum(weights, axis=1))
return temp_vector
In [16]:
%%time
direct.push({'notebook_path': os.getcwd()})
parallel_result = [parallel_estimate(tree, obs, x) for x in
zip(lat_chunks, lon_chunks)]
temp_vector = np.c_[[x.result for x in parallel_result]]
In [24]:
ax = sampler.plot_data(lats, lons, np.reshape(temp_vector, lons.shape))
sampler.plot_data(obs.lat, obs.lon, obs.temp, S=50, ax=ax, cbar=False)
Out[24]:
In [17]: